/*
 * @lc app=leetcode.cn id=98 lang=javascript
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    let stack=[]
    let order=-Infinity
    while(stack.length||root!==null){
      while(root!==null){
          stack.push(root)
          root=root.left
      }
      root=stack.pop()
      if(root.val<=order)return false
      order=root.val
      root=root.right
    }
    return true
};
// @lc code=end

var isValidBST = function(root) {
    let helper=function(node,lower,upper){
        if(!node)return true
        if(node.val>lower&&node.val<upper){
            return helper(node.left,lower,node.val)&&helper(node.right,node.val,upper)
        }else{
            return false
        }
    }
    return helper(root,-Infinity,Infinity)
};//递归法